package com.cn.wanxi.servlet.back.news;

import com.cn.wanxi.model.NewsTypeModel;
import com.cn.wanxi.service.INewsTypeService;
import com.cn.wanxi.service.impl.NewsTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/back/news/type/manage")
public class NewsTypeManageServlet extends HttpServlet {
    INewsTypeService newsTypeService = new NewsTypeServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //新闻类型删除
        String ntid = req.getParameter("id");
        int id = 0;
        if (!"".equals(ntid))
            id = Integer.parseInt(ntid);
        int result = newsTypeService.deleteNewsType(id);
        resp.getWriter().print(result);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //新闻类型修改
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        NewsTypeModel newsTypeModel = new NewsTypeModel();
        String nid = req.getParameter("id");
        int id = 0;
        if (!"".equals(nid))
            id = Integer.parseInt(nid);
        newsTypeModel.setId(id);
        newsTypeModel.setName(req.getParameter("name"));
        newsTypeModel.setHref(req.getParameter("href"));
        newsTypeModel.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date().getTime()));
        int result = newsTypeService.editNewsType(newsTypeModel);
        resp.getWriter().print(result);
    }
}
